package com.runewbie.pafcmall.coupon.service.impl;

import com.runewbie.common.constant.CouponConstant;
import com.runewbie.common.to.MemberPrice;
import com.runewbie.common.to.SkuReductionTo;
import com.runewbie.pafcmall.coupon.entity.MemberPriceEntity;
import com.runewbie.pafcmall.coupon.entity.SkuLadderEntity;
import com.runewbie.pafcmall.coupon.service.MemberPriceService;
import com.runewbie.pafcmall.coupon.service.SkuLadderService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.runewbie.common.utils.PageUtils;
import com.runewbie.common.utils.Query;

import com.runewbie.pafcmall.coupon.dao.SkuFullReductionDao;
import com.runewbie.pafcmall.coupon.entity.SkuFullReductionEntity;
import com.runewbie.pafcmall.coupon.service.SkuFullReductionService;


@Service("skuFullReductionService")
public class SkuFullReductionServiceImpl extends ServiceImpl<SkuFullReductionDao, SkuFullReductionEntity> implements SkuFullReductionService {

    @Autowired
    SkuLadderService skuLadderService;
    @Autowired
    MemberPriceService memberPriceService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuFullReductionEntity> page = this.page(
                new Query<SkuFullReductionEntity>().getPage(params),
                new QueryWrapper<SkuFullReductionEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 保存 sku的优惠、满减等信息 pafcmall_sms->sms_sku_ladder\sms_sku_full_reduction\sms_member_price
     * @param skuReductionTo
     */
    @Override
    public void saveSkuReduction(SkuReductionTo skuReductionTo) {
        // 1、sms_sku_ladder
        SkuLadderEntity skuLadder = new SkuLadderEntity();
        skuLadder.setSkuId(skuReductionTo.getSkuId());
        skuLadder.setFullCount(skuReductionTo.getFullCount());
        skuLadder.setDiscount(skuReductionTo.getDiscount());
        skuLadder.setAddOther(skuReductionTo.getCountStatus());
        // 只有设置了数量满减才保存
        if (skuReductionTo.getFullCount()>0){
            skuLadderService.save(skuLadder);
        }

        // 2、sms_sku_full_reduction
        SkuFullReductionEntity skuFullReduction = new SkuFullReductionEntity();
        BeanUtils.copyProperties(skuReductionTo,skuFullReduction);
        //只有设置了价格满减信息才保存满减数据
        if (skuReductionTo.getFullPrice().compareTo(new BigDecimal(0)) == 1){
            this.save(skuFullReduction);
        }

        // 3、sms_member_price
        List<MemberPrice> memberPrices = skuReductionTo.getMemberPrice();

        List<MemberPriceEntity> memberPriceEntities = memberPrices.stream().map(item -> {
            MemberPriceEntity priceEntity = new MemberPriceEntity();
            priceEntity.setSkuId(skuReductionTo.getSkuId());
            priceEntity.setMemberLevelId(item.getId());
            priceEntity.setMemberLevelName(item.getName());
            priceEntity.setMemberPrice(item.getPrice());
            //默认可叠加其他优惠
            priceEntity.setAddOther(CouponConstant.MemberPriceEnum.ADD_OTHER.getCode());

            return priceEntity;
        }).filter(item->{
            // 过滤掉没有设置会员价格的数据
            return item.getMemberPrice().compareTo(new BigDecimal(0)) == 1;
        }).collect(Collectors.toList());

        memberPriceService.saveBatch(memberPriceEntities);
    }

}